<template>
  <div>
    <top current="work"></top>
    <div class="main-content plat">
      <sidebar ref="sidebar" @childEvent="showCurrentTab" title="1" :tab="currentTab"></sidebar>
      <div class="plat-loan" v-if="currentTab=='workplace'">
        <plat-content @drawlist="toDrawList"></plat-content>
      </div>
      <div class="plat-loan" v-else-if="currentTab=='repaylist'">
        <plat-repay :key="repayKey" :drawdownId="selectedDrawdownId" @backNext="backDrawList"
                    @completed="resetSidebarHeight"></plat-repay>
      </div>
      <div class="plat-loan" v-else-if="currentTab=='drawlist'">
        <plat-loan :key="loanKey" :fundCode="selectedFundCode" @back="toRepayList"
                   @completed="resetSidebarHeight"></plat-loan>
      </div>
      <div class="plat-loan" v-else-if="currentTab=='companyInfo'">
        <div class="main-content" style="margin-top: 0">
          <supply-info actionType="1"></supply-info>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {mapState} from 'vuex'
import Top from '@/components/layout/top';
import sidebar from "../../layout/sidebar";
import platContent from "./platContent";
import platLoan from "./zsbank/platLoan";
import platRepay from "./zsbank/platRepay";
import supplyInfo from "../../process/supplyInfo";
import {queryProductItemsNoLogin} from '@/api/product/product';
import {queryCreditApplyStep, creditApplyStatus} from '@/api/authen/agreed';

export default {
  name: "plat",
  data() {
    return {
      loanKey: 1,
      selectedFundCode: '',
      repayKey: 1,
      selectedDrawdownId: '',
      currentTab: 'workplace',//对应内容tab
    }
  },
  components: {
    Top, sidebar, platContent, platLoan, platRepay, supplyInfo/*,error*/
  },

  computed: mapState({
    user(state) {
      return state.user;
    }
  }),

  mounted() {
    this.initPage();
  },
  methods: {
    toRepayList(e) {
      this.loanKey = new Date().getTime();
      this.selectedDrawdownId = e.drawdownNo;
      this.currentTab = 'repaylist';
    },
    /***
     * 查询授信具体步骤
     */
    queryCreditStatus() {
      let _ = this;
      let uscCode = this.user.user.uscCode ? this.user.user.uscCode : '';
      creditApplyStatus({
        "fundCode": '',
        "uscCode": uscCode,
        "productCode": ''
      }).then(response => {
        if (response.code == 200) {
          if (response.data) {
            if (response.data.step != 'step7') {
              _.queryProductList(response.data.productCode)
            } else {
              _.$router.push({
                path: '/plat'
              });
            }
          }
        }
      });
    },
    queryProductList(productCode) {
      let _ = this;
      queryProductItemsNoLogin().then(res => {
        if (res.code === 200) {
          let fundCode = '';
          for (let i = 0; i < res.data.length; i++) {
            if (res.data[i].productCode == productCode) {
              fundCode = res.data[i].fundCode
            }
          }
          _.$router.push({
            path: '/creditStep',
            query: {
              code: fundCode,
              productCode: productCode
            }
          });
        }
      });
    },
    initPage() {
      let _ = this;
      let id = this.user.user.id ? this.user.user.id : '';
      queryCreditApplyStep(id).then(res => {
        if (res.code == 200) {
          if (res.data) {
            switch (res.data) {
              case "0":
              case "1":
              case "2":
              case "3":
              case "4":
              case "21":
                _.$router.push({
                  path: '/creditStep'
                });
                break;
              case "6":
                _.$router.push({
                  path: '/plat'
                });
                break;
              case "5":
                _.queryCreditStatus()
                break;
            }
          }
        }
      })
    },
    showCurrentTab(e) {
      this.currentTab = e.current;
    },
    toDrawList(e) {
      this.currentTab = "drawlist";
      this.loanKey = new Date().getTime();
      this.selectedFundCode = e.fundCode;
    },
    backDrawList() {
      this.currentTab = "drawlist";
      this.loanKey = new Date().getTime();
    },
    resetSidebarHeight() {
      this.$refs.sidebar.changeHeight();
    }
  }
}

</script>

<style lang="scss">
.plat {
  background: #f8f8f8;

  .plat-loan {
    float: left;
    width: 1048px;
  }
}

.plat-client_manger {
  padding: 12px 12px 12px 36px;
}
</style>
